//
//  BLInReportCheckViewController.m
//  blanket
//
//  Created by Mac on 2018/10/31.
//  Copyright © 2018 stoneobs@icloud.com. All rights reserved.
//

#import "BLInReportCheckViewController.h"
#import "BLInReportTableViewCell.h"
#import "BLInReportModel.h"
@interface BLInReportCheckViewController ()
@property(nonatomic, strong) STButton                     *timeButton;/**< 时间 */
@property(nonatomic, strong) NSMutableArray<BLInReportModel*>                     *dataSouce;/**<  */
@property(nonatomic, strong) NSDate                     *date;/**< <##> */
@end

@implementation BLInReportCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"入库复核";
    [self configSubView];
    __weak typeof(self) weakSelf =  self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendMyRepertoryRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.tableView.separatorStyle = UITableViewCellEditingStyleNone;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
    self.timeButton = [[STButton alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 44)
                                                     title:@"日期 "
                                                titleColor:FirstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage st_imageWithNamed:@"down_gray" witdhEqualHeight:22]];
    self.timeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.timeButton makeImageRight];
    [header addSubview:self.timeButton];
    [self.timeButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedTimeButton];
    }];
    ({
        STLabel * lable = [self defultlableWithTitle:@"项目"];
        lable.top = self.timeButton.bottom + 10;
        lable.left = 30 + 0 * (UIScreenWidth - 60)/4;
        [header addSubview:lable];
        header.height = lable.bottom;
        [lable st_showLeftLine:lable.height];
    });
    ({
        STLabel * lable = [self defultlableWithTitle:@"规格"];
        lable.top = self.timeButton.bottom + 10;
        lable.left = 30 + 1 * (UIScreenWidth - 60)/4;
        [header addSubview:lable];
        header.height = lable.bottom;
        [lable st_showLeftLine:lable.height];
    });
    ({
        STLabel * lable = [self defultlableWithTitle:@"数量"];
        lable.top = self.timeButton.bottom + 10;
        lable.left = 30 + 2 * (UIScreenWidth - 60)/4;
        [header addSubview:lable];
        header.height = lable.bottom;
        [lable st_showLeftLine:lable.height];
    });
    ({
        STLabel * lable = [self defultlableWithTitle:@"备注"];
        lable.top = self.timeButton.bottom + 10;
        lable.left = 30 + 3 * (UIScreenWidth - 60)/4;
        [header addSubview:lable];
        header.height = lable.bottom;
        [lable st_showLeftLine:lable.height];
        [lable st_showRightLine:lable.height];
    });
    self.tableView.tableHeaderView = header;
    
}
- (STLabel*)defultlableWithTitle:(NSString*)title{
    CGFloat sectionLableWith = (UIScreenWidth - 60)/4;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, sectionLableWith, 40)
                                                     text:title
                                                textColor:BL_secendTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [titleLable st_showTopLine];
    [titleLable st_showBottomLine];
    titleLable.backgroundColor = UIColor.whiteColor;
    
    return titleLable;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return BLInReportTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLInReportTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLInReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    BLInReportModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedTimeButton{
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtDatePickerWithHandle:^(NSDate *date) {
        [self.timeButton setTitle:date.st_yyyyMMddByLineWithDate forState:UIControlStateNormal];
        [self.timeButton makeImageRight];
        [self.tableView.mj_header beginRefreshing];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
#pragma mark --NetWork Method
- (void)sendMyRepertoryRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"api/hotel/storage"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.date) {
         [dic setObject:self.date.st_yyyyMMddByLineWithDate forKey:@"time"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * data = responseObject[@"data"];
                                                 self.dataSouce = NSMutableArray.new;
                                                 for (NSDictionary * modelDic in data) {
                                                     BLInReportModel * model = [BLInReportModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 self.dataSouce = NSMutableArray.new;
                                                 [self.tableView reloadData];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
